#include "node.h"

Node::Node(int x,int y,int pid) : Eleman(x,y,10,10)
{
    nid=pid;
    box(FL_NO_BOX);
    nMovable=true;
    nSelectable=true;
    nDeletable=true;
    nToLink=true;
    nFromLink=true;
    nStatus=true;
    sildelete=false;
    resim=new Fl_PNG_Image("resim/node.png");
    image(resim);
    box(FL_NO_BOX);
    nitwid=new nodeProWidget(650,60,x,y-56,nid);
    nitwid->_wStatus_(nStatus);
    nitwid->_wDeletable_(nDeletable);
    nitwid->_wMovable_(nMovable);
    nitwid->_wSelectable_(nSelectable);
    nitwid->_wFromLink_(nFromLink);
    nitwid->_wToLink_(nToLink);
    nitwid->sahip(this);
    nitgoster=true;
    position(x-w()/2,y-h()/2);
}

Node::~Node()
{
    delete resim;
}

void Node::nitelikGoster(bool p)
{
    nitgoster=p;
}
std::string Node::tipgonder()
{
    return "node";
}

int Node::handle(int e)
{
    Cetele *p=(Cetele *)parent();
    Form *pp=(Form *)p->parent()->parent();
    Node *gecici;
    static float butx=0;
    static float buty=0;
    switch(e)
    {
        case FL_PUSH:
            butx=x()-Fl::event_x();
            buty=y()-Fl::event_y();
            if(Fl::event_button1() && Fl::event_ctrl())
            {
                Cizik *ciz=new Cizik(x(),y(),p->getcizid());
                ciz->nodeAll(this);
                ciz->cFirst(this->id());
                gidencizgitut.push_back(ciz);
                p->cizgiAl(ciz);
                p->add(ciz);
                p->setcizid();
                pp->redraw();
            }
            break;
        case FL_DRAG:
            int xx2=round(butx+Fl::event_x());
            int yy2=round(buty+Fl::event_y());
            if(Fl::event_button1() && !Fl::event_ctrl())
            {
                if(xx2>0 && xx2<650 && yy2>60 && yy2<510)
                {
                    this->position(xx2,yy2);
                    for(int i=0;i<p->tumnodetut.size();i++)
                    {
                        gecici=p->tumnodetut[i];
                        if(gecici!=this && gecici->x()-5<this->x() && gecici->x()+5>this->x())
                        {
                            this->position(gecici->x(),y());
                            fl_line(x(),gecici->y(),x(),this->y());
                            fl_line_style(FL_SOLID,2,"");
                            fl_color(FL_DARK_GREEN);
                        }
                        if(gecici!=this && gecici->y()-5<this->y() && gecici->y()+5>this->y())
                        {
                            this->position(x(),gecici->y());
                            fl_line(this->x(),y(),gecici->x(),y());
                            fl_line_style(FL_SOLID,2,"");
                            fl_color(FL_DARK_GREEN);
                        }
                    }
                    for(int i=0;i<gidencizgitut.size();i++)
                    {
                        gidencizgitut[i]->cizdir1();
                    }
                    for(int i=0;i<gelencizgitut.size();i++)
                    {
                        gelencizgitut[i]->cizdir2();
                    }
                }
                nitwid->pozisyon(x(),y()-56);
                nitwid->redraw();
            }
            break;
        case FL_FOCUS:
            if(resim)
                delete resim;
            resim=new Fl_PNG_Image("resim/nodes.png");
            image(resim);
            if(nitgoster)
            {
                nitwid->_wStatus_(nStatus);
                nitwid->_wDeletable_(nDeletable);
                nitwid->_wMovable_(nMovable);
                nitwid->_wSelectable_(nSelectable);
                nitwid->_wFromLink_(nFromLink);
                nitwid->_wToLink_(nToLink);
                pp->remove(pp->prop());
                pp->add(nitwid);
                pp->prosecim(nitwid);
                pp->redraw();
            }
//            pp->textX(0);
//            pp->textY(0);
//            pp->textW(0);
//            pp->textH(0);
//            pp->textR(0);
//            pp->textG(0);
//            pp->textB(0);
//            pp->textAlpha(0);
//            pp->textScrollv(0);
//            pp->textScrollh(0);
//            pp->textPage()->deactivate();
//            pp->altpro()->value(pp->genelPage());
            break;
        case FL_UNFOCUS:
            if(resim)
                delete resim;
            resim=new Fl_PNG_Image("resim/node.png");
            image(resim);
            break;
        case FL_KEYDOWN:
            if(Fl::event_key()==FL_Delete)
                sildelete=true;
            break;
    }
    return Eleman::handle(e);
}

int Node::id()
{
    return nid;
}
void Node::pozisyon(int px,int py)
{
    nx=px;
    ny=py;
}
int Node::xx()
{
    return nx;
}
int Node::yy()
{
    return ny;
}
void Node::status(bool pStatus)
{
    nStatus=pStatus;
}
bool Node::status()
{
    return nStatus;
}
void Node::selectable(bool pSelectable)
{
    nSelectable=pSelectable;
}
bool Node::selectable()
{
    return nSelectable;
}
void Node::movable(bool pMovable)
{
    nMovable=pMovable;
}
bool Node::movable()
{
    return nMovable;
}
void Node::fromLink(bool pFromLink)
{
    nFromLink=pFromLink;
}
bool Node::fromLink()
{
    return nFromLink;
}
void Node::toLink(bool pToLink)
{
    nToLink=pToLink;
}
bool Node::toLink()
{
    return nToLink;
}
void Node::deletable(bool pDeletable)
{
    nDeletable=pDeletable;
}
bool Node::deletable()
{
    return nDeletable;
}
void Node::gelensil(Cizik *p)
{
    Cetele *pp=(Cetele *)parent();
    std::vector<Cizik *>::iterator yedek;
    for(int i=0;i<gelencizgitut.size();i++)
    {
        if(p==gelencizgitut[i])
        {
            yedek=gelencizgitut.begin()+(i);
            gelencizgitut.erase(yedek);
            pp->cizgiSil(p);
            break;
        }
    }
}
void Node::gidensil(Cizik *p)
{
    Cetele *pp=(Cetele *)parent();
    std::vector<Cizik *>::iterator yedek;
    for(int i=0;i<gidencizgitut.size();i++)
    {
        if(p==gidencizgitut[i])
        {
            yedek=gidencizgitut.begin()+(i);
            gidencizgitut.erase(yedek);
            pp->cizgiSil(p);
            break;

        }
    }
}
void Node::gelencizgiAl(Cizik *p)
{
    if(p)
        gelencizgitut.push_back(p);
}
bool Node::deletesil()
{
    return sildelete;
}
nodeProWidget * Node::nitelik()
{
    return nitwid;
}
